package org.uugu.iws.common.interceptor;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Element;

import java.util.List;

/**
 * 权限验证拦截器
 *
 * Created by Silence on 14/11/6.
 */
public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> {

    public AuthInterceptor(){

        super(Phase.PRE_INVOKE);
    }

    @Override
    public void handleMessage(SoapMessage soapMessage) throws Fault {
        List<Header> headerList = soapMessage.getHeaders();
        if(headerList != null || headerList.size() < 1){
            throw new Fault(new IllegalArgumentException("No Header!!"));
        }

        Element element = (Element)headerList.get(0).getObject();

        String userId = element.getElementsByTagName("userId").item(0).getTextContent();

        // Can do anything u want.
    }
}
